package com.briup.ptt.util;

import com.briup.ptt.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

/**
 * @author 杰瑞
 * @date 2025/8/19
 */
@Component
public class EmailUtil {

    @Autowired
    private MailSender mailSender;


    //发送验证码
    public String sendEmail(String email, String emailCode){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1544635931@qq.com");
        message.setTo(email);
        message.setText("你的验证码为 "+emailCode +" 一分钟内有效");
        mailSender.send(message);
        return "已发送";
    }
    //生成验证码，并将验证码缓存

    @CachePut(value = "emailCode", key = "#email")
    public String generatorCode(String email) {
        String emailCode = String.valueOf((int)(Math.random() * 1000000));
        return emailCode;
    }

    @Cacheable(value = "emailCode", key = "#email")
    public String getCacheCode(String  email){
        return null;
    }

}
